package com.ysh.web.controller.system;


import com.github.pagehelper.PageInfo;
import com.ysh.domain.system.Dept;
import com.ysh.utils.BeanUtil;
import com.ysh.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/*
    部门控制层Controller
 */
@WebServlet("/system/dept")
public class DeptServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取参数
        String operation = req.getParameter("operation");
        // 获取页面页码
        if ("list".equals(operation)){
            list(req, resp);
        }else if ("toAdd".equals(operation)){
            toAdd(req,resp);
        }else if ("save".equals(operation)){
            save(req,resp);
        }else if ("toEdit".equals(operation)){
            toEdit(req,resp);
        }else if ("edit".equals(operation)){
            edit(req,resp);
        }else if ("delete".equals(operation)){
            delete(req,resp);
        }

    }

    /*
        删除操作
     */
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 获取前台id
        // 将数据获取到，封装成一个对象
        Dept dept = BeanUtil.fillBean(req,Dept.class);
        // 调用业务层接口
        // DeptService deptService = new DeptServiceImpl();
        deptService.delete(dept);

        // 跳转回页面list
        // list(req,resp);
        resp.sendRedirect(req.getContextPath()+"/system/dept?operation=list");


    }


    /*
        修改操作
     */
    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 将数据获取到，封装成一个对象
        Dept dept = BeanUtil.fillBean(req,Dept.class,"yyyy-MM-dd");
        // 调用业务层接口
        // DeptService deptService = new DeptServiceImpl();
        deptService.update(dept);

        // 跳转回页面list
        // list(req,resp);
        resp.sendRedirect(req.getContextPath()+"/system/dept?operation=list");
    }

    /*
        跳转到修改页面,并带入数据
        根据id查询信息，并将结果带入页面中进行回显
     */
    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 查询要修改的数据findById
        String id = req.getParameter("id");
        // DeptService deptService = new DeptServiceImpl();
        // 根据id查询结果
        Dept dept = deptService.findById(id);
        // 将数据加载到指定区域，供页面获取
        req.setAttribute("dept",dept);
        // 加载所有的部门信息放入deptList
        List<Dept> all = deptService.findAll();
        req.setAttribute("deptList",all);
        // 存储转发到update.jsp页面
        req.getRequestDispatcher("/WEB-INF/pages/system/dept/update.jsp").forward(req,resp);
    }

    /*
        添加操作
     */
    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 将数据获取到，封装成一个对象
        Dept dept = BeanUtil.fillBean(req,Dept.class,"yyyy-MM-dd");
        // 调用业务层接口
        // DeptService deptService = new DeptServiceImpl();
        deptService.save(dept);
        // 跳转回页面list
        // list(req,resp);
        resp.sendRedirect(req.getContextPath()+"/system/dept?operation=list");
    }

    /*
        跳转到添加页面
     */
    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 加载所有的部门信息，放入deptList
        List<Dept> all = deptService.findAll();
        // 存入数据
        req.setAttribute("deptList",all);
        // 跳转页面
        req.getRequestDispatcher("/WEB-INF/pages/system/dept/add.jsp").forward(req,resp);

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 进入列表页
        // 获取数据
        // DeptService deptService = new DeptServiceImpl();
        // 默认设置，当前页1
        int page = 1;
        // 默认设置，每页条数5
        int size = 5;
        // 判断页面是否获取值，有值，按页面参数进行分页
        // 当前页面
        if (StringUtils.isNotBlank(req.getParameter("page"))){
            page = Integer.parseInt(req.getParameter("page"));
        }
        // 每页显示数据条数
        if (StringUtils.isNotBlank(req.getParameter("size"))){
            page = Integer.parseInt(req.getParameter("size"));
        }
        // 指定参数进行分页查询
        PageInfo all = deptService.findAll(page, size);
        // 将数据保存到指定的位置
        req.setAttribute("page",all);
        // 跳转页面
        req.getRequestDispatcher("/WEB-INF/pages/system/dept/list.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
